<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-cascader
          style="width: 100%"
          v-model="form.parentNo"
          :options="tableData"
          @change="handleChange"
          :props="{
            value: 'id',
            label: 'goodTypeName',
            children: 'child',
            expandTrigger: 'hover',
            checkStrictly: true,
          }"
          placeholder="选择上级类别"
          ref="typeCascader"
          clearable
        >
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.status"
          placeholder="请选择状态"
        >
          <el-option label="启用" value="1"></el-option>
          <el-option label="停用" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加类别</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'child', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="goodTypeNo" label="类别编码"></el-table-column>
        <el-table-column prop="goodTypeName" label="类别名称">
        </el-table-column>
        <el-table-column prop="addTime" label="添加时间"> </el-table-column>
        <el-table-column prop="sort" label="排序"> </el-table-column>
        <el-table-column label="状态" width="80px" align="center">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.status == 1"
              >启用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.status == 2"
              >停用</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="90px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item v-if="$checkPermission(102)" command="edit"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item v-if="$checkPermission(103)" command="remove"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="90px" :model="form" size="medium">
        <el-form-item label="编码" required>
          <el-input
            v-model.trim="form.goodTypeNo"
            maxlength="20"
            placeholder="请输入编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="类别名称" required>
          <el-input
            v-model.trim="form.goodTypeName"
            maxlength="10"
            placeholder="请输入类别名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="上级类别">
          <el-tree
            ref="tree"
            node-key="id"
            :accordion="true"
            :data="tableData"
            show-checkbox
            :check-strictly="true"
            @check="check"
            :props="{
              label: 'goodTypeName',
              children: 'child',
            }"
            style="margin-top: 5px !important"
          ></el-tree>
        </el-form-item>
        <el-form-item label="排序" required>
          <el-input-number
            v-model="form.sort"
            :min="1"
            :max="100"
            :step="1"
            step-strictly
            style="width: 100%"
          ></el-input-number>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="类别编码">
          {{ lockData.goodTypeNo }}
        </el-descriptions-item>
        <el-descriptions-item label="类别名称">
          {{ lockData.goodTypeName }}
        </el-descriptions-item>
        <el-descriptions-item label="排序">
          {{ lockData.sort }}
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          {{ lockData.status == 1 ? '启用' : '停用' }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formSearch: { parentNo: 0 },
      total: null,
      tableData: [],
      visible: false,
      title: '添加类别',
      form: {},
      isLook: false,
      lockData: {},
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表数据
      this.$axios
        .post('/admin/goods/type/page', this.formSearch)
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data || []
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    search() {
      //搜索
      this.itemList()
    },
    add() {
      //添加
      this.title = '添加类别'
      this.form = { parentNo: '0', status: '1' }
      this.visible = true
      this.$nextTick(function () {
        this.$refs.tree.setCheckedKeys([])
      })
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          this.lockData = row
          this.isLook = true
          break
        case 'edit':
          this.title = '编辑类别'
          this.form = JSON.parse(JSON.stringify(row))
          this.$nextTick(() => {
            this.$refs.tree.setCheckedKeys([row.parentNo])
          })
          this.visible = true
          break
        case 'remove':
          this.$confirm('您确定要删除吗', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios
                .get('/admin/goods/type/del?id=' + row.id)
                .then((data) => {
                  if (data.code == 200) {
                    this.itemList()
                    this.$message.success('操作成功！')
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.goodTypeNo) {
        this.$message.error('请输入编码')
        return
      }
      if (!this.form.goodTypeName) {
        this.$message.error('请输入类别名称')
        return
      }
      if (!this.form.sort) {
        this.$message.error('请输入排序')
        return
      }
      const node = this.$refs.tree.getCheckedNodes()
      this.form.parentNo = node.length == 0 ? 0 : node[0].id
      if (this.form.id == this.form.parentNo) {
        this.$message.error('上级菜单不能是当前菜单')
        return
      }
      this.$axios.post('/admin/goods/type/edit', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    handleChange(val) {
      this.$refs.typeCascader.dropDownVisible = false //监听值发生变化就关闭它
      if (val) {
        this.formSearch.parentNo = val[val.length - 1]
      } else {
        this.formSearch.parentNo = 0
      }
    },
    check(a, b) {
      //监听checkbox改变事件，从而控制只选择一个节点
      if (b.checkedKeys.length > 0) {
        this.$refs.tree.setCheckedKeys([a.id])
      }
    },
  },
}
</script>
